package com.cjm.one.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cjm.one.goods.mapper.GoodsCartMapper;
import com.cjm.one.goods.mapper.GoodsSkuMapper;
import com.cjm.one.goods.service.GoodsCartService;
import com.cjm.one.model.goods.entity.GoodsCart;
import com.cjm.one.model.goods.vo.GoodsSkuCartVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsCartServiceImpl extends ServiceImpl<GoodsCartMapper, GoodsCart>
    implements GoodsCartService {
    @Resource
    private GoodsCartMapper goodsCartMapper;
    @Resource
    private GoodsSkuMapper goodsSkuMapper;

    @Override
    public Page<GoodsSkuCartVo> getCartList(Integer current, Integer size, Long id) {
        Page<GoodsSkuCartVo> goodsSkuPage = new Page<>(current, size);
        Page<GoodsSkuCartVo> goodsSkuVoPage = goodsSkuMapper.selectPageByCart(goodsSkuPage, id);
        return goodsSkuVoPage;
    }

    @Override
    public Long addCart(GoodsCart cart) {
        LambdaQueryWrapper<GoodsCart> goodsCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsCartLambdaQueryWrapper.eq(GoodsCart::getCustomerId, cart.getCustomerId())
                .eq(GoodsCart::getSkuId, cart.getSkuId());
        GoodsCart one = goodsCartMapper.selectOne(goodsCartLambdaQueryWrapper);
        if (one != null) {
            cart.setGoodsNum(one.getGoodsNum() + cart.getGoodsNum());
            cart.setId(one.getId());
            boolean b = saveOrUpdate(cart);

            return b?one.getId():null;
        }
        boolean b = saveOrUpdate(cart);
        return b?cart.getId():null;
    }

    @Override
    public Boolean updateCart(GoodsCart cart) {
        return goodsCartMapper.updateById(cart)>0;
    }

    @Override
    public Boolean deleteCart(Long id, Long customerId) {
        LambdaQueryWrapper<GoodsCart> goodsCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsCartLambdaQueryWrapper.eq(GoodsCart::getId, id)
                .eq(GoodsCart::getCustomerId, customerId);
        return goodsCartMapper.delete(goodsCartLambdaQueryWrapper) > 0;
    }

    @Override
    public Boolean deleteCartAll(Long customerId) {
        return removeById(customerId);
    }

    @Override
    public Boolean deleteCartBatch(Long[] ids, Long customerId) {
        LambdaQueryWrapper<GoodsCart> goodsCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        goodsCartLambdaQueryWrapper.in(GoodsCart::getId, ids)
                .eq(GoodsCart::getCustomerId, customerId);
        int delete = goodsCartMapper.delete(goodsCartLambdaQueryWrapper);
        return delete > 0;
    }

    @Override
    public List<GoodsCart> selectByCustomerId(Long id) {
        LambdaQueryWrapper<GoodsCart> eq = new LambdaQueryWrapper<GoodsCart>()
                .eq(GoodsCart::getCustomerId, id)
                .eq(GoodsCart::getSelected, 1);
        return goodsCartMapper.selectList(eq);
    }

    @Override
    public Page<GoodsSkuCartVo> selectByIds(Long[] ids, Long customId) {
        Page<GoodsSkuCartVo> goodsSkuCartVoPage = new Page<>();
        List<GoodsSkuCartVo> data = goodsSkuMapper.selectListByIds(ids,customId);
        goodsSkuCartVoPage.setRecords(data);
        goodsSkuCartVoPage.setTotal(data.size());
        goodsSkuCartVoPage.setPages(data.size());
        goodsSkuCartVoPage.setSize(data.size());
        goodsSkuCartVoPage.setCurrent(1);
        return goodsSkuCartVoPage;
    }
}